home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_200
/
221_01
/
peephole.c
< prev
next >
Wrap
Text File
|
1980-01-01
|
16KB
|
784 lines
/*#define VAX*/
#define PHASE2
/*#define PHASE3*/
#define P15nov86
#define P17nov86
/*#define P18nov86*/
#define P28nov86
#ifdef VAX
#include <stdio.h>
#include <ctype.h>
#else
#include stdio.h
extern int fputs(), strncmp(), strncpy();
#endif
#define save_line_count 20
#define lin_buf_len 1600
#define line 80
#define opt_deps 100
/* Peephole optimizer for 6809 code */
/* written by Dieter H. Flunkert */
/* Written: */
/* Version A.07 */
/* 02-dec-85 */
/* Update: */
/* 01-jul-86 */
/* 17-jul-86 */
/* 07-nov-86 */
/* 15-nov-86 */
/* 17-nov-86 */
/* 18-nov-86 */
/* 28-nov-86 */
/* Global variables */
char new_table[] = "EQNELEGELTGTHIHSLSLO";
char jump_table[] = "NEEQGTLTGELELSLOHIHS";
char number[] = " LB ";
#ifdef VAX
FILE *ifp, *ofp;
#else
int ofp, ifp;
#endif
char buffer[line];
char save_buffer[lin_buf_len];
char save_jump[line];
char num_1[line];
char num_2[line];
#ifdef P18nov86
int opt_level, wr_ok;
#endif
int opt_end, opt_id, bp;
/* for stand alone */
/* #define EOF -1 */
/* #define NULL 0 */
/* #define EOL 13 */
#define jump7 "*+7\n"
#define jump5 " BRA *+5\n"
#define kommas ",S\n"
#define ldd " LDD "
#define lddnum " LDD #"
#define ldd0 " LDD #0\n"
#define ldd1 " LDD #1\n"
#define pulsd " PULS D\n"
#define pshsd " PSHS D\n"
#define pshsy " PSHS Y\n"
#define pulsx " PULS X\n"
#define pshsx " PSHS X\n"
#define tfryd " TFR Y,D\n"
#define addd " ADDD "
#define addd1 " ADDD #1\n"
#define adddsplus " ADDD ,S++\n"
#define coma " COMA\n"
#define comb " COMB\n"
#define subd " SUBD "
#define stb " STB "
#define ldb " LDB "
#define stbsplus " STB [,S++]\n"
#define ldbsplus " LDB [,S++]\n"
#define lddsplus " LDD [,S++]\n"
#define stdsplus " STD [,S++]\n"
#define stdx " STD ,X\n"
#define std " STD "
#define leay " LEAY "
#define leax " LEAX "
#define cmpd " CMPD "
#define cmpd0 " CMPD #0\n"
#define cmpdsplus " CMPD ,S++\n"
#define lb " LB"
#define lbeqcc " LBEQ cc"
#define lbnecc " LBNE cc"
#define lbsr " JSR "
#define jmp " JMP "
#ifdef VAX
main()
#else
main(argc, argv) int argc, argv[];
#endif
{
/* i points to first free place in save buffer */
/* bp points to next line to be transfered to check buffer */
int i,j;
int opt;
#ifdef VAX
init();
#else
init(argc,argv);
#endif
opt_end = opt_id = bp = i = 0;
opt =
#ifdef P18nov86
wr_ok =
#endif
1;
while(getline() != EOF) { /* read lines until end of file */
if(i+line >= lin_buf_len) { /* buffer full? */
write_buffer(); /* write line to file */
i=m1_down(); /* move buffer down */
bp=0; /* reset buffer pointer */
}
i = copy_append(i); /* copy to save buffer (append it) */
while((bp = copy_buffer(bp)) > 0) { /* copy to check buffer */
if(buffer[0] != ";") { /* process only valid assembler code */
if(check_1(opt++) != 0) { /* check if good for optimize */
#ifdef P18nov86
if(wr_ok)
#endif
write_buffer(); /* write first line of buffer to file */
#ifdef P18nov86
if(!wr_ok) {
m1_down(); /* shift buffer one line */
m1_down();
}
#endif
i = m1_down(); /* shift buffer one line */
opt_end = bp = 0;
opt =
#ifdef P18nov86
wr_ok =
#endif
1;
}
else {
/* if good for optimize and already the last in this sequence */
/* write the optimized version and forget the rest */
if (opt_end < 0) {
write_opt();
opt =
#ifdef P18nov86
wr_ok =
#endif
1;
i = move_buffer(bp); /* move buffer down */
opt_end = opt_id = bp = 0;
}
}
}
}
}
if (save_buffer[bp] != '\0')
fputs(save_buffer[bp],ofp); /* write the rest of the save buffer */
fputs(buffer, ofp); /* write last line */
}
copy_buffer(bp)
/* copy line of save buffer to check buffer */
int bp; /* buffer pointer of save buffer */
{
int i;
if (bp < 0) bp = -bp; /* use absolute value */
if (save_buffer[bp] == '\0')
return (-bp); /* return negative value if nothing to do */
i = 0;
while((buffer[i++] = save_buffer[bp++]) != '\n') ;
buffer[i] = '\0';
return(bp);
}
write_buffer()
/* write first line from save buffer to output file */
{
copy_buffer(0);
fputs(buffer,ofp);
}
m1_down()
/* move save buffer one line 'down' */
{
int i;
i = 0;
while (save_buffer[i++] != '\n') ;
return(move_buffer(i));
}
move_buffer(bp)
int bp;
/* move buffer bp characters down */
{
int j;
j = 0;
while((save_buffer[j++] = save_buffer[bp++]) != '\0') ;
return (--j);
}
write_opt()
/* write optimize string to output file */
{
int i,len;
i = 0;
switch(opt_id) {
#ifdef PHASE2
case 1: {
fputs(num_2,ofp);
fputs(num_1,ofp);
break;
}
#endif
case 2: {
fputs(leax,ofp);
fputs(num_1,ofp);
fputs(ldd,ofp);
fputs(num_2,ofp);
fputs(stdx,ofp);
break;
}
case 3: {
fputs(number,ofp);
fputs(num_2,ofp);
break;
}
case 10: {
fputs(ldd,ofp);
fputs(num_1,ofp);
break;
}
case 11: {
fputs(leay,ofp);
fputs(num_2,ofp);
fputs(pshsy,ofp);
fputs(ldd,ofp);
fputs(num_1,ofp);
break;
}
#ifndef P18nov86
case 12: {
fputs(ldb,ofp);
fputs(num_2,ofp);
fputs(stb,ofp);
fputs(num_1,ofp);
break;
}
#endif
case 13: {
fputs(ldd,ofp);
fputs(num_1,ofp);
fputs(subd,ofp);
fputs(num_2,ofp);
fputs(std,ofp);
fputs(num_1,ofp);
break;
}
case 14: {
fputs(ldb,ofp);
fputs(num_1,ofp);
break;
}
case 15: {
fputs(lbsr,ofp);
fputs(num_2,ofp);
fputs(std,ofp);
fputs(num_1,ofp);
break;
}
#ifndef P18nov86
case 16: {
fputs(ldd,ofp);
fputs(num_2,ofp);
fputs(std,ofp);
fputs(num_1,ofp);
break;
}
#endif
case 17: {
fputs(ldd,ofp);
fputs(num_1,ofp);
fputs(addd,ofp);
fputs(num_2,ofp);
fputs(std,ofp);
fputs(num_1,ofp);
break;
}
case 18:
fputs(save_jump,ofp);
fputs("8\n",ofp);
fputs(ldd0,ofp);
fputs(jmp,ofp);
fputs(num_2,ofp);
/*
fputs(ldd1,ofp);
*/
break;
#ifdef P17nov86
case 21:
fputs(lddnum,ofp);
fputs(num_1,ofp);
putc('\n',ofp);
break;
#endif
#ifdef P28nov86
case 22:
fputs(num_1,ofp);
fputs(num_2,ofp);
break;
#endif
case 23:
fputs(num_1,ofp);
break;
case 24:
fputs(lddnum,ofp);
putc('-',ofp);
fputs(num_1,ofp);
break;
#ifdef P18nov86
case 99:
fputs(num_2,ofp);
fputs(num_1,ofp);
break;
#endif
default: break;
}
}
#ifdef VAX
init()
/* get filenames and open files */
{
int i; /* just for counting */
char infile[80]; /* file names */
char outfile[80];
i = 0;
fputs("\nFile to optimize? ",stdout);
while((infile[i]=getchar()) != '\n')
if(infile[i]!='\b') ++i;
else {
if(--i < 0) i = 0;
putc(' ',stdout);
putc('\b',stdout);
}
infile[i] = '\0';
if ((ifp = fopen(infile,"r")) == NULL) {
fputs("\n*** E R R O R *** opening file ",stdout);
fputs(infile,stdout);
exit(0);
}
i = 0;
fputs("\nOutput File Name? ",stdout);
while((outfile[i]=getchar()) != '\n')
if(outfile[i]!='\b') ++i;
else {
if(--i < 0) i=0;
putc(' ',stdout);
putc('\b',stdout);
}
outfile[i] = '\0';
if(outfile[0] == '\0') {
ofp = stdout;
return;
}
if ((ofp = fopen(outfile,"w")) == NULL) {
fputs("\n*** E R R O R *** opening file ",stdout);
fputs(outfile,stdout);
exit(0);
}
}
#else
init(argc, argv) int argc, argv[]; {
char *argptr;
int i;
i=0;
ifp=stdin;
ofp=stdout;
if(argc==1) {
fputs("\nusage peephole <infile >outfile",stderr);
exit(0);
}
while(--argc) {
argptr=argv[++i];
if(*argptr=='<') {
if((ifp=fopen(++argptr,"r")) == 0) {
fputs("\nerror opening input file",stderr);
exit(0);
}
}
if(*argptr=='>') {
if((ofp=fopen(++argptr,"w")) == 0) {
fputs("\nerror o